<?php
namespace app\common\model\orderdata;

use app\common\model\user\User;
use app\api\controller\Pub;
use think\Model;

class Storageareachange extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'storageareachange';
    protected $pk    = 'StorageAreaChangeId';

    const STATEINT = 1; //待处理
    const STATEOK  = 2; //通过
    const STATENO  = 3; //拒绝

    //发起人
    public function user()
    {
        return $this->belongsTo(User::class, 'UpUserId', 'UserId');
    }

    //涉及区域
    public function area()
    {
        return $this->hasOne('app\common\model\orderdata\StorageAreaGoods', 'StorageAreaGoodsId', 'StorageAreaGoodsId');
    }

    public function good()
	{
		return $this->belongsTo(Goods::class, 'GoodId', 'GoodId');
	}

    /**
     * 状态分类
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public static function getStatusText($value = '')
    {
        $status = [-1 => '全部申请', 1 => '待处理', 2 => '已通过', 3 => '已拒绝'];
        if ($value !== '') {
            return $status[$value];
        }
        return $status;
    }

    /**
     * 获取订单列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data, $type = 'mana', $is_page = true)
    {
        $rdata = new Storageareachange;
        if ($type == 'mana') {
            $rdata   = $rdata->with(['area','user']);
            $visible = ['StorageareachangeId', 'StorageAreaGoodsId', 'UpTime', 'UpUserId', 'Weight', 'Status', 'GoodId','user.Name'];
        } else  if ($type == 'ware') {
            $rdata   = $rdata->with(['area','user']);
            $rdata = $rdata->where('UpUserId',Pub::userid());
            $visible = ['StorageareachangeId', 'StorageAreaGoodsId', 'UpTime', 'UpUserId', 'Weight', 'Status', 'GoodId','user.Name'];
        } else {
            return moerr('缺少参数');
        }
        if (!empty($data['field'])) {
            $visible = array_merge($data['field'], $visible);
        }

        $option = come_model_option($data, $rdata, $is_page);

        $rdata = $option['rdata'];
        $data  = $option['data'];

        if (!empty($data['field'])) {
            $rdata = $rdata->field($visible);
        }
        $rdata = $rdata->select();
        if (!empty($visible)) {
            $visible = array_merge(['StorageareachangeId'], $visible);
            $rdata   = $rdata->visible($visible);
        }

        $ndata['list'] = $rdata->toArray();
        $ndata['statetext'] = Storageareachange::getStatusText();
        if ($is_page) {
            $ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
        }
        return $ndata;
    }

	public static function lists($data)
	{
		$list = new Storageareachange();
		$option = come_model_option($data, $list);
		$rdata = $option['rdata'];
		$data = $option['data'];
		$rdata = $rdata->select();
//		$rdata->visible($visible);

		$ndata['list'] = $rdata;

		$ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
//		$list = ['list' => $good];
//dump($ndata['list']);die;
		$status = [0 => '暂未微调', 1 => '待处理', 2 => '已通过', 3 => '已拒绝'];
		foreach ($ndata['list'] as &$v) {
			$v['Username'] = $v->user->Name;
			$v['GoodName'] = $v->good->Name;
			$v['StatusText'] = $status[$v['Status']] ?? '';
		}
		unset($v);
		$ndata['list'] = $ndata['list']->toArray();
		return $ndata;

	}

}
